{include file="public/head"/}
<style>
    [v-cloak] {
        display: none;
    }
</style>
<script src="/static/Admin/js/clipboard.js"></script>
<div class="container-fluid">
    <div class="row clearfix" id="search-box" style="margin-top:0">
        <div class="col-md-12 column text-right" v-cloak>
            <div>
                <div class="form-inline" role="form">
                    {eq name="$Think.session.admin.position_id" value="1"}
                    <div class="input-group">
                        <select class="form-control" v-model="country_id" @change="changecountry">
                            <option :value="key" v-for="(item,key) in countrys">
                                {{item}}
                            </option>
                        </select>
                    </div>
                    {/eq}
                    <div class="input-group">
                        <select class="form-control" v-model="depval">
                            <option value="">{:lang('please_select_company_department')}</option>
                            <option :value="item.id" v-for="(item,index) in deplist">
                                <template v-for="(item,index) in item.level">&nbsp;&nbsp;&nbsp;&nbsp;</template>
                                {{item.langname}}
                            </option>
                        </select>
                    </div>

                    <div class="input-group">
                        <select class="form-control" v-model="posival">
                            <option value="">{:lang('please_select_a_job')}</option>
                            <option :value="item.id" v-cloak v-for="(item,index) in posilist">
                                <template v-for="(item,index) in item.level">&nbsp;&nbsp;&nbsp;&nbsp;</template>
                                {{item.langname}}
                            </option>
                        </select>
                    </div>

                    <div class="form-group">
                        <label for="name">{:lang('search_content')}</label>
                        <input type="text" class="form-control" v-model="dname" @keyup.13="soufun" id="dname"
                               placeholder="{:lang('please_enter_with_account,_nickname,_name_or_job')}">
                    </div>
                    <div class="form-group">
                        <button type="button" class="btn btn-primary" @click="soufun"><span
                                class="glyphicon glyphicon-search"></span>{:lang('search')}
                        </button>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="row clearfix">
        <div class="col-md-12 column">
            <div class="alert alert-dismissable alert-info">
                <button type="button" class="close">×</button>
                <p></p>
            </div>
        </div>
    </div>
    <div class="ntDesktop">

        <div class="ntDesktop-head">{:lang('employees_list')}</div>

        <div class="row clearfix">
            <div class="col-md-6 column">
                <ul class="nav nav-tabs">
                    <li>
                        <a href="#home" data-toggle="tab" @click.prevent="statuschange('')">{:lang('all')}</a>
                    </li>
                    <li class="active">
                        <a href="#home" data-toggle="tab" @click.prevent="statuschange(1)">{:lang('on_the_job')}</a>
                    </li>
                    <li>
                        <a href="#home" data-toggle="tab" id="list-lock" @click.prevent="statuschange(3)">{:lang('locking')}</a>
                    </li>
                    <li>
                        <a href="#home" data-toggle="tab" id="list-leave" @click.prevent="statuschange(2)">{:lang('departure')}</a>
                        <!-- <a href="#home" data-toggle="tab" id="list-leave" @click.prevent="statuschange(2)">666</a> -->
                    </li>
                </ul>
            </div>
            <div class="col-md-6 column text-right">
                <div>
                    <a href="/admin/Organization/personneladd" class='btn btn-primary' data-toggle="modal"
                       data-target="#worker_editModal"
                       stytle="margin-right:20px"><span class="glyphicon glyphicon-plus"></span> {:lang('add_a_new_employees')}</a>
                    <span style="padding:10px">&nbsp;</span>
                    <button type="button" class="btn btn-default option-lock"
                            @click="batchFun('/admin/Organization/personnelock','lock')" v-if="status!=3"><span
                            class="glyphicon glyphicon-lock"></span> {:lang('batch_locking')}
                    </button>
                </div>
            </div>
        </div>

        <div class="row clearfix">
            <div class="col-md-12 column">
                <table class="table table-striped table-hover ntTable" v-cloak>
                    <thead>
                    <tr>
                        <th style="text-align:center"><input type="checkbox" @click="allsel"/></th>
                        <th> {:lang('account')}</th>
                        <th> {:lang('nickname/name')}</th>
                        <th> {:lang('department')}</th>
                        <th> {:lang('job')}</th>
                        <th> {:lang('gender')}</th>
                        <th class="sorting" @click="sortfun(1)" data-order="1"> {:lang('age')}</th>
                        <th> {:lang('language')}</th>
                        <th class="sorting" @click="sortfun(2)" data-order="1"> {:lang('date_of_entry')}</th>
                        <th class="sorting" @click="sortfun(3)" data-order="1"> {:lang('longin')}</th>
                        <th class="sorting" @click="sortfun(4)" data-order="1"> {:lang('edit')}</th>
                        <th style="text-align: center;"> {:lang('operate')}</th>
                    </tr>
                    </thead>
                    <tbody>
                    <tr v-for="item in list" :key="item.id+status" v-if="list.length>0">
                        <td align="center"><input type="checkbox" @click="onesel(item.id)"/></td>
                        <td align="left"> {{item.name}}</td>

                        <td v-if="item.nickname==item.username"> {{item.nickname}}</td>
                        <td v-else="item.nickname!=item.username"> {{item.nickname}} <br>
                            <small>{{item.username}}</small>
                        </td>
                        <td> {{item.departmentname}}</td>
                        <td> {{item.positionname}}</td>
                        <td> {{item.sex}}</td>
                        <td> {{item.age}}</td>
                        <td> {{item.langperson}}</td>
                        <td> {{item.work_day_date}}</td>
                        <td> {{item.loginip}}<br>
                            <small>{{item.login_date}}</small>
                        </td>
                        <td> {{item.mangname}} <br/>
                            <small>{{item.update_date}}</small>
                        </td>
                        <td class="text-right">
                            <div>
                                <a :href="'/admin/Organization/personnelxiangqin?id='+item.id"
                                   class='btn btn-sm btn-primary' data-toggle="modal" data-target="#worker_detailModal"
                                   onfocus="this.blur()"><span
                                        class="glyphicon glyphicon-sunglasses"></span> {:lang('details')}</a>

                                <a :href="'/admin/Organization/personneledit?id='+item.id"
                                   class='btn btn-sm btn-primary' data-toggle="modal"
                                   data-target="#worker_editModal" onfocus="this.blur()"><span
                                        class="glyphicon glyphicon-pencil"></span>
                                    {:lang('edit')}</a>
                                <a :href="'/admin/Organization/persetroot?id='+item.id" class='btn btn-sm btn-primary'
                                   data-toggle="modal" data-target="#menu2admin_editModal"><span
                                        class="fa fa-cogs"></span> {:lang('authority')}</a>
                                <a :href="'/admin/Organization/personnelreset?id='+item.id"
                                   class='btn btn-sm btn-primary option-repassword' data-toggle="modal"
                                   data-target="#worker_repasswordModal" onfocus="this.blur()"><span
                                        class="glyphicon glyphicon-retweet"></span>{:lang('reset_password')} </a>

                                <a v-if="item.status!=3" href="javascript:;" class='btn btn-sm btn-default option-lock'
                                   @click="batchFun('/admin/Organization/personnelock','lock',item.id)"><span
                                        class="glyphicon glyphicon-lock"></span>{:lang('locking')}</a>

                                <a v-if="item.status==3" href="javascript:;"
                                   class='btn btn-sm btn-default option-unlock'
                                   @click="batchFun('/admin/Organization/personnelunlock','unlock',item.id)"><span
                                        class="glyphicon glyphicon-leaf"></span>{:lang('unlock')}</a>

                                <a v-if="item.status!=2" href="javascript:;" class='btn btn-sm btn-default option-leave'
                                   @click="batchFun('/admin/Organization/personneleave','leave',item.id)"><span
                                        class="glyphicon glyphicon-erase"></span>{:lang('departure')}</a>

                                <a v-if="item.status==2" href="javascript:;"
                                   class='btn btn-sm btn-default option-recover'
                                   @click="batchFun('/admin/Organization/personneresume','resume',item.id)"><span
                                        class="glyphicon glyphicon-briefcase"></span>{:lang('reinstate')}</a>

                                <a href="javascript:;" class='btn btn-sm btn-default option-clear'
                                   @click="batchFun('/admin/Organization/personnedelete','delete',item.id)"><span
                                        class="glyphicon glyphicon-fire"></span>{:lang('clear')}</a>
                            </div>
                        </td>
                    </tr>
                    <tr style="text-align: center;" v-if="list.length==0">
                        <td colspan="12">{:lang('no_data')}</td>
                    </tr>
                    </tbody>
                </table>
            </div>
        </div>

        <div class="row clearfix">
            <div class="col-md-12 column">
                <ul class="pagination" id="pagination">
                </ul>
            </div>
        </div>

    </div>
</div>

<!-- InstanceEndEditable --></div>

<!--添加弹窗部分 开始-->
<div class="modal fade" id="worker_editModal" tabindex="-1" role="dialog" aria-labelledby="ntModalLabel"
     aria-hidden="true" data-backdrop="static" data-keyboard="false">
    <div class="modal-dialog modal-lg" >
        <div class="modal-content"  style="width:1000px">

        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>


<!-- 员工资料详情modal窗 -->
<div class="modal fade" id="worker_detailModal" tabindex="-1" role="dialog" aria-labelledby="ntModalLabel"
     aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content"  style="width:1000px">

        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>

<!-- 密码重置modal窗 -->
<div class="modal fade" id="worker_repasswordModal" tabindex="-1" role="dialog" aria-labelledby="ntModalLabel"
     aria-hidden="true"  data-backdrop="static" data-keyboard="false">
    <div class="modal-dialog " >
        <div class="modal-content"  style="width:700px">

        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>

<div class="modal fade" id="menu2admin_editModal" tabindex="-1" role="dialog" aria-labelledby="ntModalLabel"
     aria-hidden="true"  data-backdrop="static" data-keyboard="false">
    <div class="modal-dialog modal-lg">
        <div class="modal-content" style="width:1000px">

        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>

<!--弹窗部分 结束-->
{include file="public/foot"/}

<script>
    Vue.directive('focus', {
        bind: function (el) {
            el.focus()
        },
        inserted: function (el) {
            el.focus();//自动获取焦点
        },
    })
    var table = new Vue({
        el: '.container-fluid',
        data: {
            list: [],//数据
            page: 1,//分页
            pageSize: 2,//条数
            status: 1,
            batch: [],//ids
            allid: [],//当前页ids
            order: 1,
            lang: 'zh',//网站默认语言
            pauname: '',
            deplist: [],//部门
            posilist: [],//职位
            countrys: [],
            depval: '',//部门查询
            posival: '',//职位查询
            dname: '',//账号查询
            country_id: '0',
        },
        methods: {
            setPage(pageCurrent, pageSum) {
                //数据总条数
                var myPageCount = pageSum;
                //一页显示多少条
                var myPageSize = this.pageSize;
                //总页数
                var countindex = myPageCount % myPageSize > 0 ? (myPageCount / myPageSize) + 1 : (myPageCount / myPageSize);
                $.jqPaginator('#pagination', {
                    totalPages: parseInt(countindex),
                    //最多显示多少页
                    visiblePages: 5,
                    //默认显示第几页
                    currentPage: pageCurrent,
                    first: '<li class="first"><a href="javascript:;">'+langcon.page_home+'</a></li>',
                    prev: '<li class="prev"><a href="javascript:;"><i class="arrow arrow2"></i>'+langcon.previous_page+'</a></li>',
                    next: '<li class="next"><a href="javascript:;">'+langcon.next_page+'<i class="arrow arrow3"></i></a></li>',
                    last: '<li class="last"><a href="javascript:;">'+langcon.last_page+'</a></li>',
                    page: '<li class="page"><a href="javascript:;">{{page}}</a></li>',
                    all:'<li class="page"><a href="javascript:;">'+parseInt(countindex)+' / '+this.page+'PG , '+this.pageSize+' /PG</a></li>',
                    onPageChange: (num, type) => {
                        if (type == "change") {
                            this.page = num
                            this.getAllList()
                        }
                    }
                });
            },
            //数据所有获取
            getAllList(){
                $('table input').prop('checked', false)
                this.batch = [];
                $.ajax({
                    url: "/admin/organization/personnel",
                    type: 'post',
                    data: {
                        page: this.page,//分页
                        pageSize: this.pageSize,//条数
                        status: this.status,
                        department_id: this.depval,//部门查询
                        position_id: this.posival,//职务查询
                        dname: this.dname,//账号，昵称，姓名
                    },
                    dataType: "json",
                    success: (result) => {
                        this.list = result.getmanage;//员工
                        this.deplist = result.frames//部门
                        this.posilist = result.roles//职位
                        this.countrys = result.country
                        this.lang = result.lang//网站语言
                        this.allid = result.dataids;
                        // 将数据渲染到页面
                        if (result.size / this.pageSize > 1) {
                            $('#pagination').show()
                            this.setPage(this.page, result.size)
                        } else {
                            $('#pagination').hide()
                        }
                    }
                })
            },
            //切换状态
            statuschange(status){
                this.status = status;
                this.page = 1;
                this.getAllList(1);
            },
            //切换国家
            changecountry(){
                //获取对应的职位
                this.getframepos('position', 1);
                //获取对应的部门
                this.getframepos('department', 2);
            },
            //获取对应国家的职位
            getframepos(table, code){
                $.ajax({
                    url: '/admin/api/postionfrmaes',
                    data: {
                        country_id: this.country_id,
                        table: table,
                        lang: this.lang
                    },
                    type: 'post',
                    dataType: 'json',
                    success: (data) => {
                        if (table == 'position') {
                            //职位
                            this.posilist = data;
                        } else {
                            //部门
                            this.deplist = data;
                        }
                    }
                })
            },
            onesel(did){
                if (this.batch.indexOf(did) == -1) {
                    this.batch.push(did)
                } else {
                    this.batch.splice(this.batch.indexOf(did), 1)
                }
                var isall = true
                $('td input').each(function () {
                    if (!$(this).prop('checked')) {
                        isall = false
                    }
                })
                $('th input').prop('checked', isall)
            },
            //全选
            allsel(event){
                if ($(event.target).prop('checked')) {
                    $(event.target).parents('table').find('input').prop('checked', true)
                    this.batch = this.allid
                } else {
                    $(event.target).parents('table').find('input').prop('checked', false)
                    this.batch = []
                }
            },
            batchFun(requrl, reqact, did){
                var data = {
                    act: reqact,
                    id: this.batch.join(',')
                }
                if (did) {
                    data.id = did
                } else {
                    data.id = this.batch.join(',')
                }
                $.ajax({
                    url: requrl,
                    data,
                    type: 'post',
                    success: (data) => {
                        data = JSON.parse(data)
                        change_mes_type('.alert',data.status)
                        if (data.status != 1) {
                            $('.alert').show()
                            $('.alert p').html('*' + data.msg + '<br>')
                            return false;
                        } else {
                            $('.alert').show()
                            $('.alert p').html('*' + data.msg + '<br>')
                            if(this.page > Math.ceil(this.list.length-1 / this.pageSize)){
                                if(this.page>1){
                                    this.page--
                                }
                            }
                            this.getAllList()
                        }
                    }
                })
            },
            sortoneData(a, b) {
                if (this.order == 1) {
                    $(event.target).attr('data-order', 2)
                    return b[this.pauname] - a[this.pauname]
                } else {
                    $(event.target).attr('data-order', 1)
                    return a[this.pauname] - b[this.pauname]
                }
            }
            ,
            sortfun(type, $event){ 
                this.order = $(event.target).attr('data-order')
                if (type == 1) {
                    this.pauname = 'age'
                    this.list.sort(this.sortoneData);
                }
                if (type == 2) {
                    this.pauname = 'work_day'
                    this.list.sort(this.sortoneData);
                }
                if (type == 3) {
                    this.pauname = 'login_time'
                    this.list.sort(this.sortoneData);
                }
                if (type == 4) {
                    this.pauname = 'update_at'
                    this.list.sort(this.sortoneData);
                }
            },
            soufun(){
                this.getAllList(1)
            }
        },
        created(){
            this.getAllList(1);
            $('#worker_editModal').on('hide.bs.modal', () => {
                this.getAllList()
            })
        }
    })
    $("#worker_editModal").on("hide.bs.modal", function () {
        $(this).removeData("bs.modal");
        $(".modal-content").children().remove();
        $(document).unbind('keydown')
    });
    $("#worker_detailModal").on("hide.bs.modal", function () {
        $(this).removeData("bs.modal");
        $(document).unbind('keydown')
    });
    $("#worker_repasswordModal").on("hide.bs.modal", function () {
        $(this).removeData("bs.modal");
        $(".modal-content").children().remove();
        $(document).unbind('keydown')
    });
    $("#menu2admin_editModal").on("hide.bs.modal", function () {
        $(this).removeData("bs.modal");
        $(".modal-content").children().remove();
        $(document).unbind('keydown')
    });
</script>
